package com.estar.assistsurvy.activity;

import java.util.ArrayList;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.MainPageViewpagerAdapter;
import com.estar.assistsurvy.fragment.HelpPageFragment;
import com.estar.assistsurvy.fragment.HomePageFragment;
import com.estar.assistsurvy.fragment.HomePageFragments;
import com.estar.assistsurvy.fragment.MinePageFragment;
import com.estar.assistsurvy.service.ImageService;
import com.estar.assistsurvy.service.getLocationService;
import com.estar.assistsurvy.widget.NotScrollViewpager;
import com.iflytek.sunflower.FlowerCollector;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends BaseActivity implements OnCheckedChangeListener,OnClickListener{

	private NotScrollViewpager MainViewpager;
	private TextView title;
	private ImageView homePage_img;
	private Intent intent;
	private HomePageFragments homePages;
	private HelpPageFragment helpPage;
	private MinePageFragment myPage;
	private ArrayList<Fragment> fragments;
	private MainPageViewpagerAdapter mainAdapter;
	private RadioButton homePage_button,helpPage_button,myPage_button;
	private SharedPreferences preferences;
	private String authenState;
	private long exitTime=0;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        JPushInterface.init(getApplicationContext());
        init();
    	FragmentManager manager=getSupportFragmentManager();
        mainAdapter=new MainPageViewpagerAdapter(manager, fragments);
        MainViewpager.setAdapter(mainAdapter);
        MainViewpager.setNotScroll(false);
        MainViewpager.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return true;
			}
		});
//		preference=getSharedPreferences("role", Context.MODE_PRIVATE);
        preferences=this.getSharedPreferences("user", Context.MODE_PRIVATE);
        authenState=preferences.getString("authenticationState", "");
        new startService().execute();
    }
    
    private class startService extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
	    	if(authenState.equals("1")){
    		intent=new Intent(MainActivity.this, getLocationService.class);
            startService(intent);
            intent=new Intent(MainActivity.this, ImageService.class);
            startService(intent);
    	}
			return null;
		}
		
	}
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	//结束service
//    	if(authenState.equals("1")){
//    		stopService(new Intent(MainActivity.this, GPSService.class));
//    		stopService(new Intent(MainActivity.this, getLocationService.class));
//    	}
//    		stopService(new Intent(MainActivity.this, ImageService.class));

    }
    
    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		FlowerCollector.onResume(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		FlowerCollector.onPause(this);
	}
    
    private void clearState(){
    	homePage_button.setChecked(false);
    	homePage_button.setTextColor(getResources().getColor(R.color.colorMaintext));
    	helpPage_button.setChecked(false);
    	helpPage_button.setTextColor(getResources().getColor(R.color.colorMaintext));
    	myPage_button.setChecked(false);
    	myPage_button.setTextColor(getResources().getColor(R.color.colorMaintext));
    }
    
    private void init(){
    	MainViewpager=find(R.id.MainviewPager);
		homePage_img=find(R.id.homePage_img);
		homePage_img.setOnClickListener(this);
		title=find(R.id.title);
    	
        fragments=new ArrayList<Fragment>();
        new HomePageFragment();
        homePages=new HomePageFragments();
        fragments.add(homePages);
        helpPage=new HelpPageFragment();
        fragments.add(helpPage);
        myPage=new MinePageFragment();
        fragments.add(myPage);

        homePage_button=find(R.id.HomePage_button);
        homePage_button.setOnCheckedChangeListener(this);
        helpPage_button=find(R.id.HelpPage_button);
        helpPage_button.setOnCheckedChangeListener(this);
        myPage_button=find(R.id.MinePage_button);
        myPage_button.setOnCheckedChangeListener(this);
        
        
    	
    }
    
    
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if(isChecked){
			switch (buttonView.getId()) {
			case R.id.HomePage_button:
				clearState();
				title.setText("人人勘");
				MainViewpager.setCurrentItem(0);
				homePage_button.setChecked(true);
				homePage_button.setTextColor(getResources().getColor(R.color.colorTextOrange));
				break;
				
			case R.id.HelpPage_button:
				clearState();
				title.setText("帮助");
				MainViewpager.setCurrentItem(1);
				helpPage_button.setChecked(true);
				helpPage_button.setTextColor(getResources().getColor(R.color.colorTextOrange));
				break;
				
			case R.id.MinePage_button:
				clearState();
				title.setText("我");
				MainViewpager.setCurrentItem(2);
				myPage_button.setChecked(true);
				myPage_button.setTextColor(getResources().getColor(R.color.colorTextOrange));
				break;

			default:
				break;
			}
		}
	}
	
	@SuppressLint("ShowToast") @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
			if(System.currentTimeMillis()-exitTime>2000){
				showShortToast("再按一次退出");
			exitTime=System.currentTimeMillis();
			}
			else{
//				preference.edit().clear().commit();
//				preference.edit().clear().commit();
//				JPushInterface.setAliasAndTags(getApplicationContext(), "", null,mAliasCallback);	
				finish();
			System.exit(0);}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
//	private final TagAliasCallback mAliasCallback = new TagAliasCallback() {  
//		@Override  
//		public void gotResult(int code, String alias, Set<String> tags) {  
//			//	        String logs ;  
//			switch (code) {  
//			case 0:  
//					          String  logs = "Set tag and alias success";  
//					            Log.i("wl", logs+"code--->"+code+"----alias--->"+alias);  
//				// 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。  
//				break;  
//			case 6002:  
//
//				break;  
//			default:  
//			}   
//		}  
//	}; 

	private boolean isClick=true;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.homePage_img:
			if(isClick){
				intent=new Intent(MainActivity.this, SidebarActivity.class);
				startActivityForResult(intent, 0);
				overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
				isClick=false;
			}
			
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		if(arg0==0&&arg1==0){
			isClick=arg2.getBooleanExtra("isClick", true);
		}
	}
	
}
